package com.huazi.eduservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huazi.commonutils.CommentResult;
import com.huazi.eduservice.client.VodClient;
import com.huazi.eduservice.entity.EduChapter;
import com.huazi.eduservice.entity.EduCourse;

import com.huazi.eduservice.entity.EduVideo;
import com.huazi.eduservice.service.EduChapterService;
import com.huazi.eduservice.service.EduCourseService;
import com.huazi.eduservice.service.EduVideoService;
import com.huazi.eduservice.vo.CourseQuery;
import com.huazi.eduservice.vo.DeleteVodUtils;
import com.huazi.eduservice.vo.FinalInfo;

import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;




/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author huazi
 * @since 2021-03-18
 */
@RestController
//@CrossOrigin
@RequestMapping("/eduservice/course")
public class EduCourseController {
    @Autowired
    EduCourseService courseService;

    //删除小节
    @Autowired
    EduVideoService videoService;
    //删除章节
    @Autowired
    EduChapterService chapterService;

    @Resource
    VodClient vodClient;

    //课程添加信息
    @PostMapping("addCourseInfo")
    public CommentResult addCourse(@RequestBody EduCourse eduCourse){
        Boolean i = courseService.saveOrUpdate(eduCourse);
//        System.out.println(eduCourse.getId());
        return  CommentResult.getOK().data("msg","添加成功！").data("id",eduCourse.getId());
    }
    //根据id获取课程信息
    @GetMapping("getOneCourse/{id}")
    public CommentResult getOneCourse(@PathVariable String id){
        EduCourse course = courseService.getOne(new QueryWrapper<EduCourse>().eq("id", id));
        return CommentResult.getOK().data("course",course);
    }
    //删除课程
    @DeleteMapping("deleteCourse/{id}")
    public CommentResult deleteCourse(@PathVariable String id){
        //查询所有的视频id
        List<EduVideo> videos = videoService.list(new QueryWrapper<EduVideo>().eq("course_id", id));
        if(videos!=null&&videos.size()>0){
            //调用方法获取相应字符串 1,2,3
            String ids = DeleteVodUtils.getStringIds(videos);
            vodClient.deleteVodVideo(ids);
        }
        //先删除小节
        videoService.remove(new QueryWrapper<EduVideo>().eq("course_id",id));
//        System.out.println("删除小节");
        //在删除章节
        chapterService.remove(new QueryWrapper<EduChapter>().eq("course_id",id));
//        System.out.println("删除章节");
        boolean b = courseService.removeById(id);
//        System.out.println("删除课程");
        return CommentResult.getOK().data("msg","删除成功！");
    }
    //回显最终发布信息
    @GetMapping("getFinalInfo/{id}")
    public CommentResult getFinalInfo(@PathVariable String id){
        FinalInfo finalInfo = courseService.getFinalInfo(id);
        return CommentResult.getOK().data("finalInfo",finalInfo);
    }
    @GetMapping("updateStatus/{id}")
    public CommentResult updateStatus(@PathVariable String id){
        courseService.updateStatus(id);
        return CommentResult.getOK().data("msg","发布成功！");
    }
    //课程分页信息查询
    @GetMapping("pageCourse/{current}/{size}")
    public CommentResult pageCourse(@ApiParam(name="current",value = "当前页",required = true) @PathVariable Integer current, @ApiParam(name="size",value = "页面记录数",required = true) @PathVariable Integer size){
        Page<EduCourse> page=new Page<>(current,size);//创建对象分页
        courseService.page(page,null);
        return getOkByError(page);
    }
    //获取所有的课程信息
    @GetMapping("getCourseInfos")
    public CommentResult getCourseInfos(){
        List<EduCourse> list = courseService.list(null);
        Page<EduCourse> page = new Page<>(1, list.size());
        page.setRecords(list).setSize(list.size()).setTotal(list.size()).setPages(1).setCurrent(1);
        return CommentResult.getOK().data("page",page);
    }
    //分页组合条件查询
    @PostMapping("pageWrapperCourse/{current}/{size}")
    public CommentResult pageWrapperCourse(
            @ApiParam(name="current",value = "当前页",required = true)
            @PathVariable Integer current,
            @ApiParam(name="size",value = "页面记录数",required = true)
            @PathVariable Integer size,
            @RequestBody(required = false) CourseQuery courseQuery){//表示对象可以没有数据
        Page<EduCourse> page=new Page<>(current,size);//创建对象分页
        QueryWrapper<EduCourse> wrapper=new QueryWrapper<>();
        //获取条件值
        String title = courseQuery.getTitle();
        String status = courseQuery.getStatus();
        LocalDateTime start = courseQuery.getStartTime();
        LocalDateTime end = courseQuery.getEndTime();
        //判断是否构成条件
        if(!StringUtils.isEmpty(title)) wrapper.like("title",title);

        if(!StringUtils.isEmpty(status)) wrapper.eq("status",status);

        if(!StringUtils.isEmpty(start)) wrapper.ge("gmt_create",start);

        if(!StringUtils.isEmpty(end)) wrapper.le("gmt_create",end);

        courseService.page(page,wrapper);

        return getOkByError(page);
    }
    public CommentResult getOkByError(Page<EduCourse> page ){
        if(page.getRecords()!=null&&page.getRecords().size()>0){
            return CommentResult.getOK().data("pageLists",page);
        }
        page.setCurrent(0).setPages(0).setRecords(new ArrayList<>()).setSize(0).setTotal(0);
        //如果没有数据则返回空数据
        return CommentResult.getOK().data("pageLists",page);
    }
    //-------------前端获取数据------------------
    //前端获取前8门课程信息
    @GetMapping("getIndexCourses/{current}/{size}")
    public CommentResult getIndexCourses(@PathVariable("current") Integer current,@PathVariable("size") Integer size){
        CommentResult indexCourses = courseService.getIndexCourses(current, size);
        return indexCourses;
    }
    //获取课程列表
    @GetMapping("getCourseLists/{current}/{size}")
    public CommentResult getCourseLists(@PathVariable("current") Integer current,@PathVariable("size") Integer size){
        CommentResult courses = courseService.getCourseLists(current, size);
        return courses;
    }
}

